import 'package:get/get.dart';
import 'package:mtf/app/data/WithdrawRecordEntity.dart';
import 'package:mtf/app/net/HttpManger.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
/**
 * GetX Template Generator - fb.com/htngu.99
 * */

class withdrawRecordController extends GetxController{
  RxList<ListWithdrawElement> withdraw1List = <ListWithdrawElement>[].obs;
  RxList<ListWithdrawElement> withdraw2List = <ListWithdrawElement>[].obs;
  RxList<ListWithdrawElement> withdraw3List = <ListWithdrawElement>[].obs;
  RxList<ListWithdrawElement> withdraw4List = <ListWithdrawElement>[].obs;

  var page = 1;

  void refreshData(RefreshController refreshController,int state) {
    page=1;
    reqWithdrawList(refreshController,state);
  }


  void loadData(RefreshController refreshController,int state) {
    page+=1;
    reqWithdrawList(refreshController,state);
  }

  RxList<ListWithdrawElement> getList(int state){
    var stateArray = [-1, 0, 1, 2];
    var listArray = [withdraw1List,withdraw2List,withdraw3List,withdraw4List];
    return listArray[stateArray.indexOf(state)];
  }

  void reqWithdrawList(RefreshController refreshController, int state) async {
    var resp = await HttpManager.reqWithdrawRecord(page, state);
    if (resp.isSuccessData()) {
      if(page==1){
        getList(state).assignAll(resp.data!.list);
        refreshController.refreshCompleted();
      }else{
        getList(state).addAll(resp.data!.list);
        if(resp.data!.list.isEmpty){
          refreshController.loadNoData();
        }else{
          refreshController.loadComplete();
        }

      }
    }
  }
}
